package thesis;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

import experiment.Dimension;


public class DataFromFile {
	public static void createToyExample3(String fileName) throws IOException {
		BufferedWriter fw = new BufferedWriter(new FileWriter(fileName));
		fw.append("1\n");
		fw.append("d1 3 1\n");
		int id = 0;
		// a
		for (int i = 0; i < 98; i++) {
			fw.append("" + (id++) + " 0 0 1 0 1 0 0 2 0\n");
		}
		// b
		for (int i = 0; i < 1; i++) {
			fw.append("" + (id++) + " 0 0 0 0 1 1 0 2 0\n");
		}
		// c
		for (int i = 0; i < 1; i++) {
			fw.append("" + (id++) + " 0 0 0 0 1 0 0 2 1\n");
		}
		fw.flush();
		fw.close();
	}
	
	public static void createToyExample2(String fileName) throws IOException {
		BufferedWriter fw = new BufferedWriter(new FileWriter(fileName));
		fw.append("1\n");
		fw.append("d1 3 1\n");
		int id = 0;
		// abc
		for (int i = 0; i < 100; i++) {
			fw.append("" + (id++) + " 0 0 1 0 1 1 0 2 1\n");
		}
		// ab
		for (int i = 0; i < 100; i++) {
			fw.append("" + (id++) + " 0 0 1 0 1 1 0 2 0\n");
		}
		// ac
		for (int i = 0; i < 100; i++) {
			fw.append("" + (id++) + " 0 0 1 0 1 0 0 2 1\n");
		}
		// a
		for (int i = 0; i < 100; i++) {
			fw.append("" + (id++) + " 0 0 1 0 1 0 0 2 0\n");
		}
		fw.flush();
		fw.close();
	}

	public static void createToyExample(String fileName) throws IOException {
		BufferedWriter fw = new BufferedWriter(new FileWriter(fileName));
		fw.append("3\n");
		fw.append("d1 1 1\n");
		fw.append("d2 1 1\n");
		fw.append("d3 1 1\n");
		int id = 0;
		// abc
		for (int i = 0; i < 100; i++) {
			fw.append("" + (id++) + " 0 0 1 1 0 1 2 0 1\n");

		}
		// ab
		for (int i = 0; i < 100; i++) {
			fw.append("" + (id++) + " 0 0 1 1 0 1 2 0 0\n");
		}
		// ac
		for (int i = 0; i < 100; i++) {
			fw.append("" + (id++) + " 0 0 1 1 0 0 2 0 1\n");
		}
		// a
		for (int i = 0; i < 100; i++) {
			fw.append("" + (id++) + " 0 0 1 1 0 0 2 0 0\n");
		}
		fw.flush();
		fw.close();
	}

	public static void getDataFromFile(String fileName,
			HashMap<Long, DataObject> memoryTweets,
			List<Dimension> dimensions,
			ArrayList<Double> weights) {
		// Location of file to read
		File file = new File(fileName);

		try {

			Scanner scanner = new Scanner(file);
			int numDim = Integer.parseInt(scanner.nextLine());
			String[] names = new String[numDim];
			int[] numValues = new int[numDim];
			for (int i = 0; i < numDim; i++) {
				names[i] = scanner.next();
				numValues[i] = scanner.nextInt();
				weights.add(scanner.nextDouble());
				Dimension dim = new Dimension();
				dimensions.add(dim);
				for (int j = 0; j < numValues[i]; j++) {
					dim.value2obj.put(names[i] + "_" + j, new HashMap<Long, Double>());
				}
			}
			String line = scanner.nextLine();
			while (scanner.hasNextLine()) {
				line = scanner.nextLine();
				StringTokenizer st = new StringTokenizer(line, " ");
				long oId = Long.parseLong(st.nextToken());
				DataObject t = new DataObject(oId);
				t.setDbId(oId);
				memoryTweets.put(oId, t);
				while (st.hasMoreTokens()) {
					int dimId = Integer.parseInt(st.nextToken());
					String vName = names[dimId] + "_"
							+ Integer.parseInt(st.nextToken());
					double prob = Double.parseDouble(st.nextToken());
					HashMap<Long, Double> rels = dimensions.get(dimId).value2obj.get(
							vName);
					rels.put(oId, prob);
				}
			}
			scanner.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
